/*
 * L1.h
 *
 *  Created on: Oct 10, 2018
 *      Author: ax
 */

#ifndef PA_L1_H_
#define PA_L1_H_

#include <stdint.h>


typedef struct {
    uint8_t  psInfoSize;
    uint8_t  threadId;
    uint8_t  optionsFlag;
    uint8_t  operation;

    uint16_t destQueue;
    uint8_t  rsvd;
    uint8_t  flowId;
} CdeCmdPkt;

typedef struct {
    uint16_t byteCount;
    uint8_t  rsvd;
    uint8_t  operation;

    uint32_t dataPointer;
} CdeCmdWd;

typedef struct {
    uint32_t mainLoopCounter;
    uint32_t packetsReceivedCount;
    uint32_t packetsMatchMac0Count;
    uint32_t packetsMatchMac1Count;
    uint32_t packetsDiscarded;
    uint32_t mac0Ms32;
    uint16_t mac0Ls16;
    uint16_t rsvd1;
    uint32_t mac1Ms32;
    uint16_t mac1Ls16;
    uint16_t rsvd2;
} BootStats;
//c0 hold this status

typedef struct {
    uint32_t pif0;

    uint8_t  pvtData1;
    uint8_t  pvtData2;
    uint8_t  validPsSize;
    uint8_t  physPsSize;

    uint8_t  pktType_pvtFlags;
    uint8_t  psFlags_errorFlags;
    uint8_t  srcId;
    uint8_t  flowIdx;

    uint16_t ctrlDataSize;
    uint16_t pktDataSize;

    uint16_t pktId;
    uint16_t destQ;

    uint32_t timestamp;

    uint32_t swinfo0;

    uint32_t swinfo1;
} PktDsc;
//c2 hold this

typedef uint32_t Ip4;

typedef struct {
    uint8_t  VerLen;
    uint8_t  Tos;
    uint16_t TotalLen;
    uint16_t Id;
    uint16_t FlagOff;
    uint8_t  Ttl;
    uint8_t  Protocol;
    uint16_t Checksum;
    Ip4      IPSrc;
    Ip4      IPDst;
    uint8_t  Options[0];
} Ip4Head;

typedef union {
    uint8_t  hex[16];
    uint16_t hex2[8];
    uint32_t wd[4];
    uint64_t ll[2];
} Ip6;

typedef struct {
    uint32_t Ver: 4;
    uint32_t Tos: 8;
    uint32_t Flg: 20;

    uint16_t Len;
    uint8_t  Ext;
    uint8_t  Hop;

    Ip6      SrcIp;
    Ip6      DstIp;
} Ip6Head;

typedef struct {
    uint16_t SrcPort;
    uint16_t DstPort;
    uint16_t Length;
    uint16_t Checksum;
} UdpHead;

typedef struct {
    uint16_t REQ;
    uint16_t ACK;
    uint16_t LEN;
    uint16_t CKSM;
} RawIp;

typedef struct {
    uint16_t HardType;
    uint16_t ProtocolType;
    uint8_t  HardSize;
    uint8_t  ProtocolSize;
    uint16_t Op;
    uint8_t  SrcAddr[6];
    Ip4      IPSrc;
    uint8_t  DstAddr[6];
    Ip4      IPDst;
} ArpPacket;

typedef struct {

} NDPPacket;

typedef union {
    struct {
        UdpHead Head;
        uint8_t Payload[];  } Udp;
    RawIp                     Raw;
} IpPacket;

typedef struct {
    struct {
        uint8_t   DstMac[6];
        uint8_t   SrcMac[6];
        uint16_t  SzTpy;                } Head;
    union {
        struct {
            Ip4Head  Head;
            IpPacket Packet;    } Ip4;
        struct {
            Ip6Head  Head;
            IpPacket Packet;    } Ip6;
        NDPPacket                 Ddp;
        ArpPacket                 Arp;  } IpPacket;
} EthFrame;

#define tStatus_Command0            t0
#define tStatus_Command1            t1
#define tStatus_Command2            t2
#define tStatus_Command3            t3
#define tStatus_CDENewPacket        t4
#define CDENewPacket                0x0010
#define tStatus_CDEHeldPacket       t5
#define tStatus_CDEBusy             t6
#define tStatus_CDEOutPacket        t7
#define tStatus_Timer               t9

// CDE definitions
#define XID_CDECTRL                 0
#define XID_CDEDATA                 1


#define CDE_CMD_ADVANCE_TO_PACKET           0x0C
#define CDE_CMD_PACKET_ADVANCE              0x20
#define CDE_CMD_ADVANCE                     0x00
#define CDE_CMD_PACKET_FLUSH                0x21

#define CDE_FLG_SET_THREADID                (1<<4)
#define CDE_FLG_SET_FLOWID                  (1<<5)
#define CDE_FLG_SET_PSINFO                  (1<<6)
#define CDE_FLG_SET_DESTQUEUE               (1<<7)


// Only destination is to the host qm
#define THREADID_CDMA               6           // Packets to CDMA

#define PA_MAGIC_ID  0x0CEC11E0

#endif /* L1_H_ */
